<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class GetAlimamaOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'get-alimama-order';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '从阿里妈妈获取订单';

    protected $url = 'http://pub.alimama.com/report/getTbkPaymentDetails.json?queryType=1&payStatus=&DownloadID=DOWNLOAD_REPORT_INCOME_NEW&startTime=2018-01-03&endTime=2018-01-09';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $key = [
            "创建时间" => 'create_time',
            "点击时间" => 'click_time',
            "商品信息" => 'title',
            "商品id" => 'tbid',
            "所属店铺" => 'shop_name',
            "商品数" => 'item_num',
            "商品单价" => 'item_price',
            "订单状态" => 'status',
            "订单类型" => 'order_type',
            "收入比率" => 'profit_rate',
            "分成比率" => 'share_rate',
            "付款金额" => 'paid_amount',
            "效果预估" => 'expected_profit',
            "结算金额" => 'settled_amount',
            "预估收入" => 'expected_share',
            "结算时间" => 'settled_time',
            "佣金比率" => 'commission_rate',
            "佣金金额" => 'commission_amount',
            "补贴比率" => 'subsidy_rate',
            "补贴金额" => 'subsidy_amount',
            "补贴类型" => 'subsidy_kind',
            "成交平台" => 'user_agent',
            "第三方服务来源" => 'third_party_service_source',
            "订单编号" => 'order_id',
            "类目名称" => 'category_name',
            "来源媒体id" => 'site_id',
            "广告位id" => 'adzone_id'

        ];
        $cookie = 't=887f418a5e3ff51627fdfad70f94892b; l=ApycOD1P--j4eIxMZ2WBXimJ7LFALUAH; undefined_yxjh-filter-1=true; cna=tPjEEZR5KnYCAa8AB1HJEZbl; account-path-guide-s1=true; 124555719_yxjh-filter-1=true; 108383452_yxjh-filter-1=true; v=0; cookie2=163cb600340f7c1083fe29e8e9ca435a; _tb_token_=e7010eee337b; alimamapwag=TW96aWxsYS81LjAgKFgxMTsgTGludXggeDg2XzY0KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvNTguMC4zMDI5LjExMCBTYWZhcmkvNTM3LjM2SEg9MTNSdW50aW1lPWtlY2VrbGRtZmJjcGpmbW5uaWpkamhjZ2dwY25rcGJoQUxJQ0ROLyBET0wvSEVMTE9fR1dGX3NfNTM1Nl9yMng5YWs0NzQxMjVfODMy; cookie32=d93bc869287267636c38f5cd32a88deb; alimamapw=TldaFncMFSAHQSdQRCdXMQNRUwICUgICAAYGU1xTAgBRWwBTBAVWAgIEVlACAAAD; cookie31=MTI0NTU1NzE5LHlhbiVFNCVCOSU5NiVFNSVBRSU5RCw2NDIzMTMzMjVAcXEuY29tLFRC; login=V32FPkk%2Fw0dUvg%3D%3D; rurl=aHR0cDovL3B1Yi5hbGltYW1hLmNvbS8%3D; isg=AhwcqM28qnunYFxSqyu61wC97TMOPcXYSgaVa_YdKYfqQbzLHqWQT5L_1Z5D';
        $data = curlGet($this->url, $cookie);
        file_put_contents('/home/woody/a.xls',$data);

        \Excel::filter('chunk')->load('/home/woody/a.xls')->chunk(500, function($results) use($key) {
            $saveData = [];
            $saveNum = 0;
            foreach($results as $row) {
                foreach ($row as $okey => $oval){
                    if(isset($key[$okey])){
                        $saveData[$saveNum][$key[$okey]] = $oval;
                    }
                }
                $saveNum ++;
            }
        });
    }
}
